package by.dvrudenko.avtobaza.webapp.page.car.panel;

import javax.inject.Inject;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.dvrudenko.avtobaza.datamodel.Car;
import by.dvrudenko.avtobaza.datamodel.CarType;
import by.dvrudenko.avtobaza.services.CarService;
import by.dvrudenko.avtobaza.webapp.page.AbstractPanel;
import by.dvrudenko.avtobaza.webapp.page.car.CreateCarPage;
import by.dvrudenko.avtobaza.webapp.page.car.CarPage;
import by.dvrudenko.avtobaza.webapp.page.car.CarTypeCreatePage;
import by.dvrudenko.avtobaza.webapp.utils.security.SecuredLinkForAdmin;


public class CarDetailsPanel extends AbstractPanel {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(CarDetailsPanel.class);
	
	@Inject
	private CarService carService;
	
	public final static String PATH_CAR_IMG = "images/car/car1.jpg";
	
	Car randomCar = carService.getRadomCar();

	public CarDetailsPanel(String id) {
		super(id);
	}

	@Override
	protected void onInitialize() {
		super.onInitialize();
		LOGGER.info("Initialize carDetailsPanel");
		WebMarkupContainer liContainer = new WebMarkupContainer("wrapper-li");
		add(liContainer);
		WebMarkupContainer hContaiener = new WebMarkupContainer("wrapper-h");
		hContaiener.add(new Label("panelCarTitle", getResourceModel("p.home.car.panel.carPanelTitle")));
		liContainer.add(hContaiener);
		// add img
		WebMarkupContainer imgContaiener = new WebMarkupContainer("wrapper-img");	
		imgContaiener.add(AttributeModifier.append("src", PATH_CAR_IMG));
		liContainer.add(imgContaiener);

		liContainer.add(new Label("carYear", new Model<Integer>(getYear(randomCar.getYear()))));
		liContainer.add(new Label("carBrand", new Model<String>(randomCar.getCarType().getBrand())));		
		liContainer.add(new Label("carModel", new Model<String>(randomCar.getCarType().getModel())));
		liContainer.add(new Label("carBody", new Model<String>(randomCar.getCarType().getBody())));
		liContainer.add(new Label("carType", new Model<String>(randomCar.getCarType().getType())));
		liContainer.add(new Label("carVolumeCapacity", new Model<Integer>(randomCar.getVolumeCapacity())));		
		liContainer.add(new Label("carCarryingCapacity", new Model<Integer>(randomCar.getCarryingCapacity())));	

		liContainer.add(new Link<Void>("carPage") {
			@Override
			public void onClick() {
				setResponsePage(CarPage.class);
			};
		});
		
		// ссылка на создание новой машины видна только админу
		Link<Void> createCar = new SecuredLinkForAdmin("createCar"){
			@Override
			public void onClick() {
				Car car = new Car();
				LOGGER.info("redirect to CreateCarPage");
				setResponsePage(new CreateCarPage(car));
			};
		};
		liContainer.add(createCar);		
		
		
		//ссылка на создание нового типа машины видна только админу
		Link<Void> createCarType = new SecuredLinkForAdmin("createCarType"){
			@Override
			public void onClick() {
				CarType carType = new CarType();
				LOGGER.info("redirect to CarTypeCreatePage");
				setResponsePage(new CarTypeCreatePage(carType));
			};
		};
		liContainer.add(createCarType);

	}

}
